package android.pot.com.smartcar.ui.my.activity;

import android.os.Bundle;
import android.pot.com.smartcar.R;
import android.pot.com.smartcar.action.Action;
import android.pot.com.smartcar.base.CommonAdapter;
import android.pot.com.smartcar.base.MyResponseHandler;
import android.pot.com.smartcar.base.ViewHolder;
import android.pot.com.smartcar.base.swipe.SwipeBackActivity;
import android.pot.com.smartcar.entity.PlatFormEntity;
import android.pot.com.smartcar.entity.TagEntity;
import android.pot.com.smartcar.tool.LoadingTool;
import android.pot.com.smartcar.tool.TipToast;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhaoshilu on 2017/11/27.
 */
public class MyJoinActivity extends SwipeBackActivity {

    private EditText et_phone;

    private int selectPos = 1;

    private String curSelect = "汽车保养店";

    private PlatFormEntity entity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_join);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        initData();
        initView();
        addListener();
    }

    @Override
    protected void initView() {
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("入驻平台");
        et_phone = (EditText) findViewById(R.id.et_phone);
        initTagView();
    }


    @Override
    protected void addListener() {
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scrollToFinishActivity();
            }
        });
        findViewById(R.id.bt_commit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getDetailData();
            }
        });

    }

    private void initTagView(){
        GridView gridView = (GridView) findViewById(R.id.gv_list);
        final List<TagEntity> list = new ArrayList<>();
        if (null !=entity.getData()){
            if (entity.getData().size()>0){
                curSelect = entity.getData().get(0);
                for (int i = 0; i<entity.getData().size();i++){
                    if (i == 0){
                        list.add(new TagEntity(entity.getData().get(i),i,true));
                    }else{
                        list.add(new TagEntity(entity.getData().get(i),i,false));
                    }
                }
            }else{
                list.add(new TagEntity("汽车保养店",0,true));
                list.add(new TagEntity("轮胎保养店",1,false));
                list.add(new TagEntity("汽车清洗店",2,false));
                list.add(new TagEntity("汽车维修店",3,false));
                list.add(new TagEntity("分销商",4,false));
            }
        }

        final CommonAdapter mAdapter = new CommonAdapter<TagEntity>(MyJoinActivity.this,list,R.layout.gridcell_tag) {
            @Override
            public void convert(ViewHolder helper, TagEntity item) {
                if (item.isSelected()){
                    helper.setTextColor(R.id.tv_type,R.color.white);
                    helper.setBackgroundResource(R.id.tv_type,R.drawable.corner_black_bg);
                }else{
                    helper.setTextColor(R.id.tv_type,R.color.gray_999999);

                    helper.setBackgroundResource(R.id.tv_type,R.drawable.corner_storke_1_ed_bg);
                }
                helper.setText(R.id.tv_type,item.getTitle());
            }
        };
        gridView.setAdapter(mAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                for (TagEntity entity:list){
                    entity.setSelected(false);
                }
                curSelect = list.get(position).getTitle();
                selectPos = position;
                list.get(position).setSelected(true);
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        entity = (PlatFormEntity) bundle.getSerializable("list");

    }


    private void getDetailData() {
        if(TextUtils.isEmpty(et_phone.getText().toString())){
            TipToast.show("请输入联系方式");
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("entering_type", curSelect);
        params.put("phone", et_phone.getText().toString());
        LoadingTool.StartLoading(this);


        Action.get().platform_stationed(this, params, new MyResponseHandler() {
            @Override
            public void onSuccess(String message, JSONObject response) {
                LoadingTool.EndLoading(MyJoinActivity.this);
                TipToast.show("已提交");
                scrollToFinishActivity();
            }
        });
    }


}